package cn.taobaoke.restful.service;

import cn.taobaoke.restful.domain.ProductEntity;
import cn.taobaoke.restful.domain.ProductRepository;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import lombok.val;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * ProductService类.
 * +-----------------------------------------------------------------------------------------------+
 * |类名称: ProductService                                                                         |
 * |创建时间: 3/19/17                                                                               |
 * |功能简介:                                                                                       |
 * |参考链接:                                                                                       |
 * +-----------------------------------------------------------------------------------------------+
 *
 * @author luch
 */
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class ProductService {

    public void createOrUpdate(final ProductEntity productEntity, final Long domainId) {
        String itemId = productEntity.getItemId();
        if (itemId == null) return;

        val dbProductOpt = productRepository.findByItemIdAndDomainId(itemId, domainId);

        ProductEntity dbProduct = null;
        if (! dbProductOpt.isPresent()) {
            productEntity.setId(null);
            dbProduct = productEntity;
            dbProduct.setDomainId(domainId);
        } else {
            dbProduct = dbProductOpt.get();
            updateDbProduct(dbProduct, productEntity);
        }
        productRepository.save(dbProduct);
    }

    public void createOrUpdate(final List<ProductEntity> productEntityList, final Long domainId) {
        if (productEntityList == null || productEntityList.isEmpty()) {
            return;
        }

        List<String> itemIdList = new ArrayList<>(productEntityList.size());
        for (ProductEntity productEntity : productEntityList) {
            if (productEntity.getItemId() != null) {
                itemIdList.add(productEntity.getItemId());
            }
        }

        if (itemIdList.size() > 0) {
            List<ProductEntity> dbProductList = productRepository.findByDomainIdAndItemIdIn(domainId, itemIdList);
            List<ProductEntity> newProductList = new ArrayList<>(productEntityList.size());

            for (ProductEntity newProduct : productEntityList) {
                boolean exists = false;
                String newItemId = newProduct.getItemId();
                for (ProductEntity dbProduct : dbProductList) {
                    if (newItemId.equals(dbProduct.getItemId())) {
                        exists = true;
                        updateDbProduct(dbProduct, newProduct);
                        break;
                    }
                }
                if (!exists) {
                    newProductList.add(newProduct);
                }
            }

            if (dbProductList.size() > 0) {
                productRepository.saveAll(dbProductList);
            }
            if (newProductList.size() > 0) {
                productRepository.saveAll(newProductList);
            }
        }
    }

    private void updateDbProduct(ProductEntity dbProduct, ProductEntity newProduct) {
        if (newProduct.getItemImage() != null) {
            dbProduct.setItemImage(newProduct.getItemImage());
        }
        if (newProduct.getCouponActivity() != null) {
            dbProduct.setCouponActivity(newProduct.getCouponActivity());
        }
        if (newProduct.getCouponAmount() != null) {
            dbProduct.setCouponAmount(newProduct.getCouponAmount());
        }
        if (newProduct.getCouponStart() != null) {
            dbProduct.setCouponStart(newProduct.getCouponStart());
        }
        if (newProduct.getCouponEnd() != null) {
            dbProduct.setCouponEnd(newProduct.getCouponEnd());
        }
        if (newProduct.getCouponLeft() != null) {
            dbProduct.setCouponLeft(newProduct.getCouponLeft());
        }
        if (newProduct.getCouponTotal() != null) {
            dbProduct.setCouponTotal(newProduct.getCouponTotal());
        }
    }

    public Page<ProductEntity> search(List<String> keywords, Pageable pageable, final Long domainId) {
        if (keywords == null || keywords.isEmpty()) {
            return productRepository.findAll(pageable);
        }


        if (keywords.size() == 1) {
            return productRepository.findByItemNameOrShopNameLike(keywords.get(0), domainId, pageable);
        }
        if (keywords.size() == 2) {
            return productRepository.findByItemNameOrItemNameLike(keywords.get(0), keywords.get(1), domainId, pageable);
        }

        // 目前最多处理三个关键字
        return productRepository.findByItemNameOrItemNameOrItemNameLike(keywords.get(0), keywords.get(1), keywords.get(2), domainId, pageable);
    }


    public ProductEntity getProductById(final Long id, final Long domainId) {
        return productRepository.findByIdAndDomainId(id, domainId).orElse(null);
    }

    public List<ProductEntity> findLimitN(final Integer id, final Integer size, final Long domainId) {
        List<ProductEntity> list = productRepository.findLimitN(id, size, domainId);
        return Collections.unmodifiableList(list);
    }

    private final @NonNull
    ProductRepository productRepository;

}
